from rest_framework import serializers

from doctor import models


class DoctorSerializer(serializers.ModelSerializer):
    job = serializers.SerializerMethodField()
    hospital = serializers.SerializerMethodField()
    department = serializers.SerializerMethodField()
    area = serializers.SerializerMethodField()

    def get_job(self, obj):
        return obj.jid.name

    def get_hospital(self, obj):
        return obj.hid.name

    def get_department(self, obj):
        return obj.did.name

    def get_area(self, obj):
        return obj.aid.name

    class Meta:
        model = models.doctor
        fields = '__all__'


class DepartmentSerializer(serializers.ModelSerializer):
    doctorlist = serializers.SerializerMethodField()

    def get_doctorlist(self, obj):
        query = models.doctor.objects.filter(did=obj.id)
        return DoctorSerializer(query, many=True).data

    class Meta:
        model = models.Department
        fields = '__all__'


class FirstSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Department
        fields = ['id', 'name', 'image']


class DepartmentDetailSerializer(serializers.ModelSerializer):
    son = FirstSerializer(many=True)

    class Meta:
        model = models.Department
        fields = '__all__'


class CitySerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Area
        fields = ['id', 'name']


class AreaSerializer(serializers.ModelSerializer):
    son = CitySerializer(many=True)

    class Meta:
        model = models.Area
        fields = '__all__'


class CommentSerializer(serializers.ModelSerializer):
    patient = serializers.SerializerMethodField()

    def get_patient(self, obj):
        return obj.pid.name

    class Meta:
        model = models.Comment
        fields = '__all__'


class PatientSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Patient
        fields = '__all__'


class QuestionSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Question
        fields = '__all__'


class PatientDetailSerializer(serializers.ModelSerializer):
    question = serializers.SerializerMethodField()

    def get_question(self, obj):
        return obj.question.content

    class Meta:
        model = models.Patient
        fields = '__all__'
